#include "reposolutiontask.h"

#include "gitutil.h"
#include "repoanalyzermanager.h"

#include <QDir>
#include <qd.h>
#include <repoutil.h>

RepoSolutionTask::RepoSolutionTask() : manual(false)
{

}


void RepoSolutionTask::setManager(RepoAnalyzerManager *newManager)
{
    manager = newManager;
}

void RepoSolutionTask::setDir(const QString &newDir)
{
    dir = newDir;
}

void RepoSolutionTask::setName(const QString &newName)
{
    name = newName;
}

void RepoSolutionTask::setPath(const QString &newPath)
{
    path = newPath;
}

void RepoSolutionTask::setManual(bool newManual)
{
    manual = newManual;
}

void RepoSolutionTask::run()
{
    QStringList questions;
    QString value = "正常";
    QString realpath = dir + QDir::separator() + ".repo/project-objects/" + name + ".git";

    if (!RepoUtil::repo_project_objects_name_exists(dir, name)) {
        questions << "缺失该对象";
    } else{
        if (!RepoUtil::repo_project_objects_name_config_validated(dir, name)) {
            questions << "缺失config文件";
        }

        if (!RepoUtil::repo_projects_path_3rlinks_validated(dir, name, path)) {
            questions << "缺失软链接";
        }

        auto v = RepoUtil::repo_info_validate(dir, name);
        if (!v.a) {
            questions << v.b;
        }

        if (RepoUtil::repo_project_objects_name_objects_pack_exists_tmp_pack_file(dir, name)) {
            questions << "可瘦身";
        }

        v = RepoUtil::query_path_git_workspace_is_ok(dir, path);
        if (!v.a) {
            questions << v.b;
        } else {
            if (!GitUtil::git_status_porcelain(RepoUtil::_paths(dir, path))) {
                questions << "已修改";
            }
        }

        if (RepoUtil::_dir_exists(dir, path, ".git", "index.lock")) {
            questions << "存在锁";
        }
    }
    qd << path << value << questions;
    emit manager->notify_solution(path, questions.isEmpty() ? value : questions.join(","), manual);
}

